package com.candy.control;

import com.alibaba.fastjson.JSON;
import com.candy.pojo.UserBean;
import com.candy.service.UserInfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/getUserInfoseByAjax")
public class UserInfoByAjaxServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("访问后台成功............");
//        设置字符集，处理中文乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//调用业务逻辑处理层进行业务处理
        UserInfoService userInfoService=new UserInfoService();
       List<UserBean> luser= userInfoService.getUserInfo();

        String strJsonList = JSON.toJSONString(luser);


//           若用户名密码正确，则跳转到登录成功页面
        // 使用out对象(PrintWriter)将响应内容，输出给ajax引擎对象
        PrintWriter printWriter = resp.getWriter();
        // 发送响应内容到浏览器的ajax引擎
        printWriter.println(strJsonList); // 发送的json字符串
        // 清空数据 【清空printwriter的缓存区的数据】
        printWriter.flush();




    }
}
